﻿using System.Collections.ObjectModel;
using System.Windows;
using HMIWebDisplay;
using Microsoft.Win32;

namespace HMIWebGhostRemover.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<DisplayFile> _displayFiles = new ObservableCollection<DisplayFile>();

        public MainWindow()
        {
            InitializeComponent();
            buttonLoadDisplay.Click += ButtonLoadDisplayClick;
            buttonRemoveGhosts.Click += ButtonRemoveGhostsClick;
            buttonSaveChanges.Click += ButtonSaveChangesClick;
            listBoxDisplays.DataContext = _displayFiles;
        }

        void ButtonLoadDisplayClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = ".htm";
            openFileDialog.Filter = "HMI Web Display (.htm)|*.htm";
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == false)
                return;

            foreach (var fileName in openFileDialog.FileNames)
                _displayFiles.Add(new DisplayFile(fileName));
        }

        void ButtonRemoveGhostsClick(object sender, RoutedEventArgs e)
        {
            foreach (var selectedItem in listBoxDisplays.SelectedItems)
            {
                ((DisplayFile)selectedItem).RemoveGhosts();
            }
        }

        void ButtonSaveChangesClick(object sender, RoutedEventArgs e)
        {
            foreach (var displayFile in _displayFiles)
            {
                displayFile.SaveChanges();
            }
        }
    }
}
